package receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import db.ServiceDatabase;

public class SmsReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context ctx, Intent intent) {
		// Retrieves a map of extended data from the intent.
		final Bundle bundle = intent.getExtras();
		ServiceDatabase db = new ServiceDatabase(ctx);
		try {
			if (bundle != null) {
				final Object[] pdusObj = (Object[]) bundle.get("pdus");

				for (int i = 0; i < pdusObj.length; i++) {
					SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
					String phone = ServiceDatabase.refinePhone(currentMessage.getDisplayOriginatingAddress());
					// String message = currentMessage.getDisplayMessageBody();
					db.updateSeen(phone, "sms");
				} // end for loop
			} // bundle is null

		} catch (Exception e) {
			Log.e("SmsReceiver", "Exception smsReceiver" + e);
		} finally {
			if (db != null)
				db.close();
		}
	}

}
